package com.jbh.fourclibrary.activity;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.jbh.fourclibrary.R;
import com.jbh.fourclibrary.fragment.CaratFragment;
import com.jbh.fourclibrary.fragment.ClarityFragment;
import com.jbh.fourclibrary.fragment.ColorFragment;
import com.jbh.fourclibrary.fragment.CutFragment;
import com.jbh.fourclibrary.fragment.ShapeFragment;
import com.jbh.fourclibrary.i.Change4CPageListener;
import com.jbh.fourclibrary.util.CurrentUtil;
import com.jbh.fourclibrary.util.Util;
import com.jbh.fourclibrary.view.ActionBarView;

public class FourCActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imShape,imCarat,imColor,imClarity,imCut;
    private FrameLayout content_1 ,content_2 ,content_3 ,content_4 ,content_5 ;
    int currentWidth ;
    int currentHeight ;
    private LinearLayout lay_content ;


    /*背景颜色*/
    private String[] bgColor = {
            "#D4D4D4",
            "#D0D0D0",
            "#DEDEDE",
            "#E3E3E3",
            "#FFFFFF",
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four_c);
        initView();
    }


    private void initView() {
        ActionBarView mActionBarView = findViewById(R.id.mActionBarView);
        mActionBarView.setBackground(Color.TRANSPARENT,Color.TRANSPARENT);
        imShape = findViewById(R.id.imShape);imShape.setOnClickListener(this);
        imCarat = findViewById(R.id.imCarat);imCarat.setOnClickListener(this);
        imColor = findViewById(R.id.imColor);imColor.setOnClickListener(this);
        imClarity= findViewById(R.id.imClarity);imClarity.setOnClickListener(this);
        imCut = findViewById(R.id.imCut);imCut.setOnClickListener(this);
        lay_content = findViewById(R.id.lay_content);
        content_1 = findViewById(R.id.content_1);
        content_2 = findViewById(R.id.content_2);
        content_3 = findViewById(R.id.content_3);
        content_4 = findViewById(R.id.content_4);
        content_5 = findViewById(R.id.content_5);
    }


    @Override
    public void onClick(View v) {
        lay_content.setGravity(Gravity.LEFT);
        int type = 0 ;
        int id = v.getId();
        if (id == R.id.imShape) {
            type = 1;
        } else if (id == R.id.imCarat) {
            type = 2;
        } else if (id == R.id.imColor) {
            type = 3;
        } else if (id == R.id.imClarity) {
            type = 4;
        } else if (id == R.id.imCut) {
            type = 5;
        }

        changeImg();
        initFragment(type);
        checkContent(type);
        if (CurrentUtil.change4CPageListener!=null){
            CurrentUtil.change4CPageListener.onPage(type);
        }
    }


    public void changeImg(){
        imShape.setBackgroundResource(R.mipmap.icon_shap_1);
        imCarat.setBackgroundResource(R.mipmap.icon_carat_1);
        imColor.setBackgroundResource(R.mipmap.icon_color_1);
        imClarity.setBackgroundResource(R.mipmap.icon_clarity_1);
        imCut.setBackgroundResource(R.mipmap.icon_cut_1);

        if (isFirst){
            isFirst = false ;
            Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_shap_1);
            currentWidth =  Util.getWidth(this) - (b.getWidth()*5+Util.dpToPx(this,60));
            currentHeight =  b.getHeight();
        }else {
            currentHeight = imColor.getMeasuredHeight();
            currentWidth =  Util.getWidth(this) - (imShape.getMeasuredWidth()
                    +imCarat.getMeasuredWidth()
                    +imColor.getMeasuredWidth()
                    +imClarity.getMeasuredWidth()
                    +imCut.getMeasuredWidth()) - Util.dpToPx(this,60);
        }
    }


    private boolean isFirst = true;
    ClarityFragment clarityFragment ;
    /**
     * @param s
     */
    private void initFragment(int s) {
        content_1.removeAllViews();
        content_2.removeAllViews();
        content_3.removeAllViews();
        content_4.removeAllViews();
        content_5.removeAllViews();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (s){
            case 1:
                transaction.replace(R.id.content_1, ShapeFragment.newInstance());
                break;
            case 2:
                transaction.replace(R.id.content_2, CaratFragment.newInstance());
                break;
            case 3:
                transaction.replace(R.id.content_3, ColorFragment.newInstance());
                break;
            case 4:
                transaction.replace(R.id.content_4, clarityFragment = ClarityFragment.newInstance());
                break;
            case 5:
                transaction.replace(R.id.content_5, CutFragment.newInstance());
                break;
        }
        transaction.commitNow();
        changeFragment(s);
    }


    public void checkContent(int type){
        switch (type){
            case 1:
                content_1.setVisibility(View.VISIBLE);
                content_1.setBackgroundResource(R.mipmap.icon_shap_bg);
                break;
            case 2:
                content_2.setVisibility(View.VISIBLE);
                content_2.setBackgroundResource(R.mipmap.icon_carat_bg);
                break;
            case 3:
                content_3.setVisibility(View.VISIBLE);
                content_3.setBackgroundResource(R.mipmap.icon_color_bg);
                break;
            case 4:
                content_4.setVisibility(View.VISIBLE);
                content_4.setBackgroundResource(R.mipmap.icon_clarity_bg);
                break;
            case 5:
                content_5.setVisibility(View.VISIBLE);
                content_5.setBackgroundResource(R.mipmap.icon_cut_bg);
                break;
        }
    }


    public void changeFragment(int type){
        reset(content_1);
        reset(content_2);
        reset(content_3);
        reset(content_4);
        reset(content_5);
        switch (type){
            case 1:
                update(content_1);
                break;
            case 2:
                update(content_2);
                break;
            case 3:
                update(content_3);
                break;
            case 4:
                update(content_4);
                break;
            case 5:
                update(content_5);
                break;
        }
    }


    public boolean isAnimating(){
        return valueAnimator!=null&&valueAnimator.isRunning();
    }


    /**
     * 更新
     * @param view
     */
    ValueAnimator valueAnimator;
    public void update(final View view){
        if (view.getMeasuredWidth()>=currentWidth)return;
        valueAnimator = ValueAnimator.ofFloat(0,1);
        valueAnimator.setDuration(200);
        valueAnimator.start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (float) animation.getAnimatedValue();
                ViewGroup.LayoutParams p = view.getLayoutParams();
                p.width = (int) (currentWidth*scale);
                p.height = currentHeight ;
                view.setLayoutParams(p);
            }
        });
    }



    public void reset(final View view){
        if (view.getMeasuredWidth()>=currentWidth){
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(1,0);
            valueAnimator.setDuration(200);
            valueAnimator.start();
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float scale = (float) animation.getAnimatedValue();
                    ViewGroup.LayoutParams p = view.getLayoutParams();
                    p.width = (int) (currentWidth*scale);
                    p.height = currentHeight ;
                    view.setLayoutParams(p);
                }
            });
        }else {
            ViewGroup.LayoutParams p = view.getLayoutParams();
            p.width = 0;
            p.height = 0 ;
            view.setLayoutParams(p);
        }
    }

    Change4CPageListener change4CPageListener;

    public void setChange4CPageListener(Change4CPageListener change4CPageListener) {
        this.change4CPageListener = change4CPageListener;
    }


}
